﻿<Cmdlet(VerbsCommon.Get, "SqlMessage")>
<[Alias]("a1")>
Public Class GetSqlMessage
    Inherits PSCmdlet

    <Parameter(ValueFromPipeline:=True, Position:=0)>
    Public Property ConnectionName As String = "Default"

    Protected Overrides Sub EndProcessing()
        MyBase.EndProcessing()

        If Not DBInfo.Connections.ContainsKey(Me.ConnectionName) Then
            WriteWarning(DBInfo.NoConnection(Me.ConnectionName))
            Exit Sub
        End If
        Try
            While DBInfo.Connections(Me.ConnectionName).HasMessages
                WriteObject(DBInfo.Connections(Me.ConnectionName).GetMessage)
            End While
        Catch nex As NotImplementedException
            WriteWarning(nex.Message)
        Catch ex As Exception
            WriteError(New ErrorRecord(ex, Nothing, ErrorCategory.InvalidOperation, Nothing))
        End Try
    End Sub

End Class
